package com.pd.modules.server.dialogs;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.security.InvalidParameterException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.pd.modules.server.database.DBUsersHandler;
import com.pd.modules.server.database.DataBase;

@SuppressWarnings("serial")
public class DialogCreateUser extends JDialog
{

	private final JPanel contentPanel = new JPanel();
	private JTextField mTxtUsername;
	private JTextField mTxtPassword;


	/**
	 * Create the dialog.
	 */
	public DialogCreateUser()
	{
		setResizable(false);
		setModalityType(ModalityType.APPLICATION_MODAL);
		setModal(true);
		setTitle("Criar novo utilizador...");
		setBounds(100, 100, 353, 104);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblUsername = new JLabel("Username:");
			lblUsername.setBounds(12, 12, 83, 16);
			contentPanel.add(lblUsername);
		}
		{
			JLabel lblPassword = new JLabel("Password:");
			lblPassword.setBounds(12, 41, 83, 16);
			contentPanel.add(lblPassword);
		}

		mTxtUsername = new JTextField();
		mTxtUsername.setBounds(94, 9, 134, 22);
		contentPanel.add(mTxtUsername);
		mTxtUsername.setColumns(10);

		mTxtPassword = new JTextField();
		mTxtPassword.setBounds(94, 41, 134, 22);
		contentPanel.add(mTxtPassword);
		mTxtPassword.setColumns(10);
		{
			JButton okButton = new JButton("Criar");
			okButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					DBUsersHandler db = DataBase.getInstance().Users();

					try
					{
						db.createUser(mTxtUsername.getText(), mTxtPassword.getText());
						JOptionPane.showMessageDialog(null, "Novo utilizador criado com sucesso!");
						setVisible(false);
					} catch (InvalidParameterException e1)
					{
						JOptionPane.showMessageDialog(null, e1.getMessage());
					}

				}
			});
			okButton.setBounds(240, 8, 95, 25);
			contentPanel.add(okButton);
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Cancelar");
			cancelButton.addActionListener(new ActionListener()
			{
				public void actionPerformed(ActionEvent e)
				{
					setVisible(false);
				}
			});
			cancelButton.setBounds(240, 37, 95, 25);
			contentPanel.add(cancelButton);
			cancelButton.setActionCommand("Cancel");
		}
	}
}
